﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BBPA.BLL
{
    public partial class Supplier
    {
        BLL.PAUser bllUser = new PAUser();
        /// <summary>
        /// 添加供货商
        /// </summary>
        /// <param name="UserState_"></param>
        /// <param name="Password_"></param>
        /// <param name="Name_"></param>
        /// <param name="ShortName_"></param>
        /// <param name="Contacts_"></param>
        /// <param name="Tel_"></param>
        /// <param name="Mobile_"></param>
        /// <param name="Address_"></param>
        /// <param name="Description_"></param>
        /// <param name="Province_"></param>
        /// <param name="City_"></param>
        /// <param name="CityNo_"></param>
        /// <param name="Area_"></param>
        /// <param name="IsLatePay_"></param>
        /// <returns></returns>
        public CallBak<int> Add(EnumHelper.UserState UserState_, string Password_, string Name_, string ShortName_, string Contacts_, string Tel_, string Mobile_, string Address_, string Description_, string Province_, string City_, string CityNo_, string Area_, bool IsLatePay_,bool IsDelivery_,int PicID_)
        {
            CallBak<int> callBak = new CallBak<int>();
            //
            if (Name_ == "") return callBak.Err("名称必须填写");
            if (Password_ != "") Password_ = StringPlus.GetRandomStr(6);
            //添加User
            Model.PAUser modelUser = new Model.PAUser();
            modelUser.UserType = (int)EnumHelper.UserType.Supplier;
            modelUser.State = (int)UserState_;
            modelUser.Username = Name_;
            modelUser.Mobile = Mobile_; 
            modelUser.Password = StringPlus.md5(Password_);
            int UserID_new = bllUser.Add(modelUser);
            //添加Supplier
            Model.Supplier modelSupplier = new Model.Supplier();
            modelSupplier.UserID = UserID_new;
            modelSupplier.Name = Name_;
            modelSupplier.ShortName = ShortName_;
            modelSupplier.Contacts = Contacts_;
            modelSupplier.Tel = Tel_;
            modelSupplier.Mobile = Mobile_;
            modelSupplier.Address = Address_;
            modelSupplier.Description = Description_;
            modelSupplier.Province = Province_;
            modelSupplier.City = City_;
            modelSupplier.Area = Area_;
            modelSupplier.IsLatePay = IsLatePay_;
            modelSupplier.IsDelivery = IsDelivery_;
            modelSupplier.PicID = PicID_;
            //Rank
            List<Model.Supplier> listModelSupplier = GetModelList("City='" + modelSupplier.City + "' order by Rank desc");
            if (listModelSupplier != null && listModelSupplier.Count > 0) modelSupplier.Rank = listModelSupplier[0].Rank + 1;
            else modelSupplier.Rank = 1;
            //Number
            modelSupplier.Number = "G" + CityNo_.PadLeft(2, '0') + modelSupplier.Rank.ToString().PadLeft(2, '0');//2
            //
            int SupplierID_new = Add(modelSupplier);
            //
            return callBak.Set(SupplierID_new);
        }
        /// <summary>
        /// 编辑 供货商
        /// </summary>
        /// <param name="modelSupplier_"></param>
        /// <param name="UserState_"></param>
        /// <param name="Password_"></param>
        /// <param name="Name_"></param>
        /// <param name="ShortName_"></param>
        /// <param name="Contacts_"></param>
        /// <param name="Tel_"></param>
        /// <param name="Mobile_"></param>
        /// <param name="Address_"></param>
        /// <param name="Description_"></param>
        /// <param name="Province_"></param>
        /// <param name="City_"></param>
        /// <param name="CityNo_"></param>
        /// <param name="Area_"></param>
        /// <param name="IsLatePay_"></param>
        /// <returns></returns>
        public CallBak<int> Update(Model.Supplier modelSupplier_, EnumHelper.UserState UserState_, string Password_, string Name_, string ShortName_, string Contacts_, string Tel_, string Mobile_, string Address_, string Description_, string Province_, string City_, string CityNo_, string Area_, bool IsLatePay_,bool IsDelivery_, int PicID_)
        {
            CallBak<int> callBak = new CallBak<int>();
            //
            if (Name_ == "") return callBak.Err("名称必须填写");
            //更新User
            Model.PAUser modelUser = bllUser.GetModel(modelSupplier_.UserID);
            modelUser.State = (int)UserState_;
            modelUser.Username = Name_;
            modelUser.Mobile = Mobile_;
            if (Password_ != "") modelUser.Password = StringPlus.md5(Password_);
            bllUser.Update(modelUser);
            //更新Supplier
            if (modelSupplier_.City != City_)
            {
                //Rank
                List<Model.Supplier> listModelSupplier = GetModelList("City='" + City_ + "' order by Rank desc");
                if (listModelSupplier != null && listModelSupplier.Count > 0) modelSupplier_.Rank = listModelSupplier[0].Rank + 1;
                else modelSupplier_.Rank = 1;
                //Number
                modelSupplier_.Number = "G" + CityNo_.PadLeft(2, '0') + modelSupplier_.Rank.ToString().PadLeft(2, '0');//2
            }
            modelSupplier_.Name = Name_;
            modelSupplier_.ShortName = ShortName_;
            modelSupplier_.Contacts = Contacts_;
            modelSupplier_.Tel = Tel_;
            modelSupplier_.Mobile = Mobile_;
            modelSupplier_.Address = Address_;
            modelSupplier_.Description = Description_;
            modelSupplier_.Province = Province_;
            modelSupplier_.City = City_;
            modelSupplier_.Area = Area_;
            modelSupplier_.IsLatePay = IsLatePay_;
            modelSupplier_.IsDelivery = IsDelivery_;
            modelSupplier_.PicID = PicID_;
            Update(modelSupplier_);
            //
            return callBak.Set(modelSupplier_.id);
        }
    }
}
